package com.paw.qrcode.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.paw.qrcode.service.IPawWxService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lvzihai
 * @date 2022/4/27
 **/
@Slf4j
@RestController
public class LoginController {



  /**
   * 微信开放平台二维码连接
   */
  private final static String OPEN_QRCODE_URL= "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=%s#wechat_redirect";

  /**
   * 开放平台回调url
   * 注意：test16web.tunnel.qydev.com 域名地址要和在微信端 回调域名配置 地址一直，否则会报回调地址参数错误
   */
//  private final static String DOMAIN_URL = "http://x5242s7821.zicp.vip";
  private final static String OPEN_REDIRECT_URL= "http://x5242s7821.zicp.vip/loginCallback";

  /**
   * 微信审核通过后的appid
   */
  private final static String OPEN_APPID= "wx9fe291deab935576";

  private String appId = "wx9fe291deab935576";
  private String appsecret = "9646b4f36c60071e5ebc2fb2753f2c69";


//  @Autowired
//  private IPawWxService pawWxService;




  @GetMapping(value = {"/", "index"})
  public String home(){
    return "welcome home";
  }


  /**
   * 拼装微信扫一扫登录url
   */
  @GetMapping("loginUrl")
  @ResponseBody
  public String loginUrl() throws UnsupportedEncodingException {

    //官方文档说明需要进行编码
    String callbackUrl = URLEncoder.encode(OPEN_REDIRECT_URL,"GBK");

    //格式化，返回拼接后的url，去调微信的二维码
    String qrcodeUrl = String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirec",OPEN_APPID,callbackUrl);

    return qrcodeUrl;
  }


  @GetMapping("/loginCallback")
  public String loginCallback(String code,String state){

    return StrUtil.format("code: {} state: {}",code,state);
  }







}
